<?php
include("../../tools/session.php");
include("../../tools/controls.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
// $page_status = $_GET["page_status"];

// 从 session 中获取的用户 ID 号
$session_id = $_SESSION["user"]["id"];
$user_id = $session_id;

$per_page = 10;

include("../../tools/mysql.php");
db_connect();

// 如果没有传入的参数，则显示页面
if (!$page_status)
{
   include("tpl-inc-wp.php");
   
   // $sql = "SELECT ceil(count(from_id)/" . $per_page . ") num FROM document WHERE from_id=" . $user_id;
   $sql = "SELECT count(from_id) num FROM document WHERE from_id=" . $user_id;
   $re = db_query_once($sql);
   
   $tpl->assign("user_id", $user_id);
   // $tpl->assign("page_num", $re["num"]);
   $tpl->assign("wholepage", $re["num"]);
   $tpl->display($oa_tpl_path . "/default/view_document.html");
}
// 如果传入的参数为 1，则显示公文记录数据 XML
elseif ($page_status == 1)
{
   header("content-type: text/xml");
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   echo "<document>\n";
   
   $page = $_POST["page"];
   // $sql = "SELECT a.zh_name, a.name, b.doc_id, b.title, b.instancy, DATE_FORMAT(b.apply_time, '%Y-%m-%d') apply_time, b.is_multi, b.is_sent, b.is_canceled, b.is_finished FROM view_doc_tpl a, document b WHERE a.tpl_id=b.tpl_id AND from_id=" . $_SESSION["user"]["id"] . " ORDER BY b.apply_time DESC ";
   $sql = "SELECT a.zh_name, a.name, b.doc_id, b.title, b.instancy, DATE_FORMAT(b.apply_time, '%Y-%m-%d') apply_time, b.is_multi, b.is_sent, b.is_canceled, b.is_finished, b.is_passed_at_last FROM view_doc_tpl a, document b WHERE a.tpl_id=b.tpl_id AND from_id=" . $_SESSION["user"]["id"] . " ORDER BY b.apply_time DESC ";
   $re = db_query_page($sql, $page, $per_page);
   
   $str = "";
   foreach($re as $v)
	 {
		$str .= "<item>\n";
		$str .= "<doc_id>" . $v["doc_id"] . "</doc_id>\n";
		$str .= "<class_name>" . $v["zh_name"] . "</class_name>\n";
		$str .= "<tpl_name>" . $v["name"] . "</tpl_name>\n";
		$str .= "<title><![CDATA[" . $v["title"] . "]]></title>\n";
		$str .= "<instancy>" . $v["instancy"] . "</instancy>\n";
		$str .= "<apply_time><![CDATA[" . $v["apply_time"] . "]]></apply_time>\n";
		$str .= "<is_multi>" . $v["is_multi"] . "</is_multi>\n";
		$str .= "<is_sent>" . $v["is_sent"] . "</is_sent>\n";
		$str .= "<is_canceled>" . $v["is_canceled"] . "</is_canceled>\n";
		$str .= "<is_finished>" . $v["is_finished"] . "</is_finished>\n";
		$str .= "<is_passed>" . $v["is_passed_at_last"] . "</is_passed>\n";
		$str .= "</item>\n";
	 }
   echo $str;
   
   echo "</document>\n";
}
// 如果传入的参数为 2，则显示指定 doc_id 的公文记录数据 XML
elseif ($page_status == 2)
{
   $doc_id = $_POST["doc_id"];
   // $doc_id = $_GET["doc_id"];
   if (empty($doc_id)) { echo "Error: 参数不足！"; exit(); } 
	 
   header("content-type: text/xml");
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   echo "<document>\n";
   
   $sql = "SELECT * FROM document WHERE from_id=" . $_SESSION["user"]["id"] . " AND doc_id=" . $doc_id;
   $re = db_query_once($sql);
   
   echo "<title><![CDATA[" . $re["title"] . "]]></title>\n";
   echo "<content><![CDATA[" . str_replace('\\"', '"', $re["content"]) . "]]></content>\n";
   
   echo "</document>\n";
}

?>

